SqrtGrad ================= 计算 Sqrt(平方根)操作的梯度。该算子是 Sqrt 算子的反向传播(backward pass)部分。 .. math:: \text{output}_i = \frac{1}{2} \times \frac{\text{input1}_i}{\text{input0}_i} 其中 `input0` 是前向传播时 Sqrt 的输出(即 :math:`y = \sqrt{x}`),`input1` 是来自后一层的上游梯度 :math:`dy`,`output` 是对原始输入 :math:`x` 的梯度 :math:`dx`。 输入: - **input0** - 前向传播时 Sqrt 的输出数据地址(即 :math:`y = \sqrt{x}`)。 - **input1** - 来自后一层的上游梯度数据地址(即 :math:`dy`)。 - **size** - 计算长度。 - **core_mask** - 核掩码(仅共享存储版本需要)。 输出: - **output** - 计算出的对原始输入的梯度数据地址(即 :math:`dx`)。 支持平台: ``FT78NE`` ``MT7004`` .. note:: - FT78NE 支持fp32 - MT7004 支持fp16, fp32 **共享存储版本:** .. c:function:: void hp_sqrt_grad_s(half* input0, half* input1, half* output, int size, int core_mask) .. c:function:: void fp_sqrt_grad_s(float* input0, float* input1, float* output, int size, int core_mask) **C调用示例:** .. code-block:: c :linenos: :emphasize-lines: 14 //FT78NE示例 #include #include int main(int argc, char* argv[]) { // 假设在DDR空间 float *input0 = (float *)0xA0000000; // Sqrt的输出 y = sqrt(x) float *input1 = (float *)0xA1000000; // 上游梯度 dy float *output = (float *)0xB0000000; // 输出梯度 dx int size = 1000; int core_mask = 0xff; fp_sqrt_grad_s(input0, input1, output, size, core_mask); return 0; } **私有存储版本:** .. c:function:: void hp_sqrt_grad_p(half* input0, half* input1, half* output, int size) .. c:function:: void fp_sqrt_grad_p(float* input0, float* input1, float* output, int size) **C调用示例:** .. code-block:: c :linenos: :emphasize-lines: 13 //FT04示例 #include #include int main(int argc, char* argv[]) { // 假设在AM空间 float *input0 = (float *)0x10000000; // Sqrt的输出 y = sqrt(x) float *input1 = (float *)0x10001000; // 上游梯度 dy float *output = (float *)0x10002000; // 输出梯度 dx int size = 1000; fp_sqrt_grad_p(input0, input1, output, size); return 0; }